<?php
class Administrator_News_Model extends BaseModel{

	public function getNewsCatForCombo(){
		$sql = 'SELECT c.id as ' . Constant::COMBOBOX_VALUE .', c.name as ' . Constant::COMBOBOX_TEXT .' FROM newscat as c where c.id != 0 order by c.name';
		return DBHelper::getInstance()->fetchAll($sql);
	}
	public function countAllNews(){
		$sql = 'SELECT count(id) as total from news';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function countAllNewsCat(){
		$sql = 'SELECT count(id) as total from newscat';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}

	public function getTopNewestNews($context, $count = null){
		if (!isset($count))
			$count = 10;
		$sql = 'SELECT id, name FROM news order by createtime DESC limit 0,' . $count;
		$result = DBHelper::getInstance()->fetchAll($sql);
		$rs = array();
		foreach ($result as $value) {
			$value['link'] = URLUltil::newsLink($context, $value[News::ID]);
			array_push($rs, $value);
		}
		return $rs;
	}
	public function getTopHotNews($context, $count = null){
		if (!isset($count))
			$count = 10;
		$sql = 'SELECT id, name FROM news order by hit DESC limit 0,' . $count;
		$result = DBHelper::getInstance()->fetchAll($sql);
		$rs = array();
		foreach ($result as $value) {
			$value['link'] = URLUltil::newsLink($context, $value[News::ID]);
			array_push($rs, $value);
		}
		return $rs;
	}
}